package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> findPage(Integer page, Integer pageSize,String name){
        Page<SetmealDto> pageInfo = setmealService.findPage(page, pageSize, name);
        return R.success(pageInfo);
    }

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmeal",key = "#setmealDto.categoryId")
    public  R<String>  add(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithSetmealDish(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 根据id查询 回显数据
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> findById(@PathVariable Long id){
        SetmealDto setmealDish = setmealService.findWithSetmealDish(id);
        return R.success(setmealDish);
    }



    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public R<String> deleteByIds(Long[] ids){
        setmealService.deleteByIds(Arrays.asList(ids));
        return R.success("删除成功");
    }

    /**
     * 批量启停
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status,Long[] ids){
        setmealService.statusUpdate(status,ids);
        return R.success("修改成功");
    }

    /**
     * 移动端页面显示套餐信息
     * @return
     */

    @GetMapping("list")
    @Cacheable(cacheNames = "setmeal",key = "#setmeal.categoryId+'_'+#setmeal.status",unless = "#result.data.size()==0")
    public R<List<Setmeal>>  list(Setmeal setmeal){
        //创建条件对象
        QueryWrapper<Setmeal> wrapper =new QueryWrapper<>();
        wrapper.eq("category_id",setmeal.getCategoryId());
        wrapper.eq("status",setmeal.getStatus());
        List<Setmeal> setmealList = setmealService.list(wrapper);
        return R.success(setmealList);
    }

    /**
     * 显示套餐里面的所有信息
     * @return
     */
    @GetMapping("/dish/{id}")
    public R<List<Dish>> dish(@PathVariable  Long id){
        Setmeal setmeal = setmealService.getById(id);

        return R.success(null);
    }


}
